﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Xom.Attributes;
using Xom.Elements;

namespace XomSample
{
    public class Catalog : XomElement, ICatalog
    {
        private IList<IBook> _books;

        /// <summary>
        /// List of all Books
        /// </summary>
        [XomBinding("book", false, false, 0, typeof(Book))]
        public IList<IBook> Books
        {
            get { return _books; }
            set
            {
                _books = value;
                RaisePropertyChanged("Books");
            }
        }

        public Catalog()
        {
            Books = new ObservableCollection<IBook>();

            //NotifyCollectionChanged 
            INotifyCollectionChanged notifylist = _books as INotifyCollectionChanged;
            notifylist.CollectionChanged += OnCollectionChanged;
        }

        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
        {
            RaiseXomCollectionChanged(this, "Books", notifyCollectionChangedEventArgs);
        }
    }

    public class Book : XomElement, IBook
    {
        private string _id;
        private string _author;
        private string _title;
        private BookGenre _genre;
        private double _price;
        private string _publishDate;
        private string _description;

        /// <summary>
        /// ID
        /// </summary>
        [XomBinding("id", true, true, 0)]
        public string Id
        {
            get { return _id; }
            set
            {
                _id = value;
                RaisePropertyChanged("Id");
            }
        }

        /// <summary>
        /// Author
        /// </summary>
        [XomBinding("author", false, true, 10)]
        public string Author
        {
            get { return _author; }
            set
            {
                _author = value;
                RaisePropertyChanged("Author");
            }
        }

        /// <summary>
        /// Title
        /// </summary>
        [XomBinding("title", false, true, 20)]
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged("Title");
            }
        }

        /// <summary>
        /// Genre
        /// </summary>
        [XomBinding("genre", false, true, 30)]
        public BookGenre Genre
        {
            get { return _genre; }
            set
            {
                _genre = value;
                RaisePropertyChanged("Genre");
            }
        }

        /// <summary>
        /// Price
        /// </summary>
        [XomBinding("price", false, true, 40)]
        public double Price
        {
            get { return _price; }
            set
            {
                _price = value;
                RaisePropertyChanged("Price");
            }
        }

        /// <summary>
        /// Publish Date
        /// </summary>
        [XomBinding("publish_date", false, true, 50)]
        public string PublishDate
        {
            get { return _publishDate; }
            set
            {
                _publishDate = value;
                RaisePropertyChanged("PublishDate");
            }
        }

        /// <summary>
        /// Description
        /// </summary>
        [XomBinding("description", false, true, 60)]
        public string Description
        {
            get { return _description; }
            set
            {
                _description = value;
                RaisePropertyChanged("Description");
            }
        }
    }
}
